import {mgeList,msgCount} from "../../request/api.js"

const state ={
    list:[],
    total:0,
    page:1,
    size:2
    
}
const mutations={
    changelist(state,arr){
        state.list = arr
    },
    changePage(state,page){
        state.page =  page
    },
    changeSize(state,size){
        state.size = size
    },
    changeTotal(state,total){
        state.total = total
    }


}
const actions ={
    asyncList(context){
        mgeList({
            size:context.state.size,
            page:context.state.page
        }).then(res=>{
            //删除数据为空的处理
            let list = res.data.list?res.data.list:[]
            if(list.length == 0 && context.state.page > 1){
                context.commit("changePage",context.state.page-1),
                context.dispatch("asyncList")
            }
            context.commit('changelist',list)
        })
    },
    //总条数
    asyncTotal(context){
        msgCount().then(res=>{
            console.log(res.data.list[0].total)
            context.commit("changeTotal",res.data.list[0].total)
        })
    },
    //页码
    asyncPage(context,page){
        context.commit("changePage",page),
        context.dispatch('asyncList')
    }
}
const getters ={
    list(state){
        return state.list
    },
    total(state){
        return state.total
    },
    size(state){
        return state.size
    }
}

export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}